/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.st.service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.jeesite.modules.st.config.RedisUtil;
import com.jeesite.modules.st.utils.BillUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeesite.common.entity.Page;
import com.jeesite.common.service.CrudService;
import com.jeesite.modules.st.entity.StPurchaseBill;
import com.jeesite.modules.st.dao.StPurchaseBillDao;
import com.jeesite.modules.st.entity.StPurchaseDetail;
import com.jeesite.modules.st.dao.StPurchaseDetailDao;

import javax.annotation.Resource;

/**
 * 采购单Service
 *
 * @author HOHD
 * @version 2020-12-16
 */
@Service
@Transactional(readOnly = true)
@Slf4j
public class StPurchaseBillService extends CrudService<StPurchaseBillDao, StPurchaseBill> {

    @Autowired
    private StPurchaseDetailDao stPurchaseDetailDao;
    @Resource
    private RedisUtil redisUtil;

    public List<StPurchaseDetail> findDetailWarehouse(StPurchaseDetail stPurchaseDetail) {
        return stPurchaseDetailDao.findDetailWarehouse(stPurchaseDetail);
    }
    public List<StPurchaseDetail> findStatistics(StPurchaseDetail stPurchaseDetail) {
        return stPurchaseDetailDao.findStatistics(stPurchaseDetail);
    }

    /**
     * 获取单条数据
     *
     * @param stPurchaseBill
     * @return
     */
    @Override
    public StPurchaseBill get(StPurchaseBill stPurchaseBill) {
        StPurchaseBill entity = super.get(stPurchaseBill);
        if (entity != null) {
            StPurchaseDetail stPurchaseDetail = new StPurchaseDetail(entity);
            stPurchaseDetail.setStatus(StPurchaseDetail.STATUS_NORMAL);
            entity.setStPurchaseDetailList(stPurchaseDetailDao.findList(stPurchaseDetail));
        }
        return entity;
    }


    public List<StPurchaseDetail> sub(StPurchaseBill stPurchaseBill) {
        List<StPurchaseDetail> sub = stPurchaseDetailDao.sub(stPurchaseBill);
        return sub;
    }

    /**
     * 查询分页数据
     *
     * @param stPurchaseBill 查询条件
     * @return
     */
    @Override
    public Page<StPurchaseBill> findPage(StPurchaseBill stPurchaseBill) {
        return super.findPage(stPurchaseBill);
    }

    @Resource
    BillUtil billUtil;

    /**
     * 保存数据（插入或更新）
     *
     * @param stPurchaseBill
     */
    @Override
    @Transactional(readOnly = false)
    public void save(StPurchaseBill stPurchaseBill) {
        if (stPurchaseBill.getIsNewRecord()) {
            String billNo = billUtil.billNo(BillUtil.BillType.purchaseBill);
            stPurchaseBill.setBillNo(billNo);
        }
        super.save(stPurchaseBill);
        // 保存 StPurchaseBill子表
        for (StPurchaseDetail stPurchaseDetail : stPurchaseBill.getStPurchaseDetailList()) {
            if (!StPurchaseDetail.STATUS_DELETE.equals(stPurchaseDetail.getStatus())) {
                stPurchaseDetail.setParentId(stPurchaseBill);
                if (stPurchaseDetail.getIsNewRecord()) {
                    stPurchaseDetail.setBillNo(stPurchaseBill.getBillNo());
                    stPurchaseDetailDao.insert(stPurchaseDetail);
                } else {
                    stPurchaseDetailDao.update(stPurchaseDetail);
                }
            } else {
                stPurchaseDetailDao.delete(stPurchaseDetail);
            }
        }
    }

    /**
     * 更新状态
     *
     * @param stPurchaseBill
     */
    @Override
    @Transactional(readOnly = false)
    public void updateStatus(StPurchaseBill stPurchaseBill) {
        super.updateStatus(stPurchaseBill);
    }

    /**
     * 删除数据
     *
     * @param stPurchaseBill
     */
    @Override
    @Transactional(readOnly = false)
    public void delete(StPurchaseBill stPurchaseBill) {
        super.delete(stPurchaseBill);
        StPurchaseDetail stPurchaseDetail = new StPurchaseDetail();
        stPurchaseDetail.setParentId(stPurchaseBill);
        stPurchaseDetailDao.deleteByEntity(stPurchaseDetail);
    }

}